﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataAccessLayer
{
    public class DBNegotiator
    {
        #region Stations
        public void AddStation(Station station)
        {
            using (var db = new TVGridEntities1())
            {
                db.Stations.Attach(station);
                db.SaveChanges();
            }
        }

        public void RemoveStation(Station station)
        {
            using (var db = new TVGridEntities1())
            {
                var st = (db.Stations.Where(s => s.ID == station.ID)).FirstOrDefault();
                db.Stations.DeleteObject(st);
                db.SaveChanges();
            }
        }

        public List<Station> GetStations()
        {
            var stations = new List<Station>();
            using (TVGridEntities1 db = new TVGridEntities1())
            {
                stations = (from s in db.Stations
                            select s).ToList();
            }

            return stations;
        }

        public Station GetStationByID(int stationID)
        {
            Station result;

            using (var db = new TVGridEntities1())
            {
                result = (from station in db.Stations
                          where station.ID == stationID
                          select station).FirstOrDefault();
            }

            return result;
        }
        #endregion

        #region Users
        public int RegisterUser(User user)
        {
            int id = 0;
            if (!string.IsNullOrEmpty(user.UserName) && !string.IsNullOrEmpty(user.Password))
            {
                using (TVGridEntities1 db = new TVGridEntities1())
                {
                    db.Users.AddObject(user);
                    UserSetting settings = new UserSetting();
                    settings.UserID = user.ID;
                    db.UserSettings.AddObject(settings);
                    db.SaveChanges();

                    id = user.ID;
                }
            }

            return id;
        }

        public int GetUserID(string userName, string password)
        {
            int id = -1;

            using (var db = new TVGridEntities1())
            {
                id = (from user in db.Users
                      where user.UserName == userName && user.Password == password
                      select user.ID).FirstOrDefault();

            }

            return id;
        }

        public List<Station> GetUserStations(int userID)
        {
            List<Station> result;

            using (var db = new TVGridEntities1())
            {
                result = (from settings in db.UserSettings 
                          join station in db.Stations
                          on settings.TVStation equals station.ID
                          where settings.UserID == userID
                          select station).ToList();

            }

            return result;
        }

        public void SaveUserStations(List<UserSetting> settings)
        {
            int tmpID =  settings.First().UserID;
            using (var db = new TVGridEntities1())
            {
                
                List<UserSetting> result = (from set in db.UserSettings
                                            where set.UserID == tmpID
                                            select set).ToList();

                if (result != null)
                {
                    foreach (var item in result)
                    {
                        db.UserSettings.DeleteObject(item);
                    }
                }
                db.SaveChanges();
                foreach (var item in settings)
                {
                    db.UserSettings.AddObject(item);
                }
                db.SaveChanges();
            }
        }
        #endregion
    }
}
